﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="AssignCharity.aspx.cs" Inherits="DonationTracker.Account.Admin.AssignCharity" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <h2>Assign Charity</h2>
    <asp:SqlDataSource ID="dsDropDown" runat="server" 
        ConnectionString="<%$ ConnectionStrings:DonationConn %>" 
        SelectCommand="SELECT [charityID], [name] FROM [charity]"></asp:SqlDataSource>
    <asp:SqlDataSource ID="dsAssignCharity" runat="server" 
        ConnectionString="<%$ ConnectionStrings:DonationConn %>" 
        DeleteCommand="DELETE FROM [donation] WHERE [donationNumber] = @donationNumber" 
        InsertCommand="INSERT INTO [donation] ([description], [charityID], [dateEntered], [enteredBy]) VALUES (@description, @charityID, @dateEntered, @enteredBy)"
        SelectCommand="SELECT [donationNumber], donor.firstName + ' ' + donor.lastName AS Donor, [description], [charityID], [dateEntered], [UserName] AS enteredBy FROM [donation] join [aspnet_Users] on donation.enteredBy = aspnet_Users.UserID
JOIN donor ON donation.donorID = donor.donorID
 WHERE ([charityID] IS NULL)" 
        
        
        UpdateCommand="UPDATE [donation] SET [charityID] = @charityID WHERE [donationNumber] = @donationNumber">
        <DeleteParameters>
            <asp:Parameter Name="donationNumber" Type="Int32" />
        </DeleteParameters>
        <InsertParameters>
            <asp:Parameter Name="description" Type="String" />
            <asp:Parameter Name="charityID" Type="Int32" />
            <asp:Parameter DbType="Date" Name="dateEntered" />
            <%--<asp:Parameter Name="enteredBy" Type="Object" />--%>
            <asp:Parameter Name="enteredBy" Type="Object" />
        </InsertParameters>
        <UpdateParameters>
            <asp:Parameter Name="charityID" Type="Int32" />
            <asp:Parameter Name="donationNumber" Type="Int32" />
        </UpdateParameters>
    </asp:SqlDataSource>
    <asp:GridView ID="gvAssignCharity" runat="server" AllowSorting="True" 
        AutoGenerateColumns="False" DataKeyNames="donationNumber" 
        DataSourceID="dsAssignCharity" onrowupdating="gvAssignCharity_RowUpdating">
        <Columns>
            <asp:CommandField ShowEditButton="True" />
            <asp:BoundField DataField="donationNumber" HeaderText="donationNumber" 
                InsertVisible="False" ReadOnly="True" SortExpression="donationNumber" />
            <asp:BoundField DataField="Donor" HeaderText="Donor" 
                SortExpression="Donor" ReadOnly="True" />
            <asp:BoundField DataField="description" HeaderText="description" 
                SortExpression="description" ReadOnly="True" />
            <asp:TemplateField HeaderText="charityID" SortExpression="charityID">
                <EditItemTemplate>
                    <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="dsDropDown" 
                        DataTextField="name" DataValueField="charityID">
                    </asp:DropDownList>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%# Bind("charityID") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="dateEntered" SortExpression="dateEntered">
                <ItemTemplate>
                    <asp:Label ID="Label2" runat="server" Text='<%# Bind("dateEntered", "{0:d}") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="enteredBy" SortExpression="enteredBy">
                <ItemTemplate>
                    <asp:Label ID="Label3" runat="server" Text='<%# Bind("enteredBy") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
    <br />
    <asp:SqlDataSource ID="dsCharitiesAssigned" runat="server" 
        ConnectionString="<%$ ConnectionStrings:DonationConn %>" 
        
        SelectCommand="SELECT [donationNumber], donor.firstName + ' ' + donor.lastName AS Donor, donation.description, charity.name AS Charity, [dateEntered], [UserName] AS enteredBy FROM [donation]
 join [aspnet_Users] on donation.enteredBy = aspnet_Users.UserID
join donor ON donation.donorID = donor.donorID 
join charity ON donation.charityID = charity.charityID
WHERE donation.charityID &gt; 0 " 
        
        UpdateCommand="UPDATE [donation] SET [charityID] = @charityID WHERE [donationNumber] = @donationNumbe">
        <UpdateParameters>
            <asp:Parameter Name="charityID" />
            <asp:Parameter Name="donationNumbe" />
        </UpdateParameters>
    </asp:SqlDataSource>
    <asp:SqlDataSource ID="dsCharitiesAssignedDropDown" runat="server" 
        ConnectionString="<%$ ConnectionStrings:DonationConn %>" 
        SelectCommand="SELECT [charityID], [name] FROM [charity]">
    </asp:SqlDataSource>
    <br /><br />
    <h2>Assigned to Charities</h2>
    <asp:GridView ID="gvCharitiesAssigned" runat="server" 
        AutoGenerateColumns="False" DataKeyNames="donationNumber" 
        DataSourceID="dsCharitiesAssigned" AllowSorting="True">
        <Columns>
            <asp:CommandField ShowEditButton="True" />
            <asp:BoundField DataField="donationNumber" HeaderText="donationNumber" 
                InsertVisible="False" ReadOnly="True" SortExpression="donationNumber" />
            <asp:BoundField DataField="Donor" HeaderText="Donor" 
                SortExpression="Donor" ReadOnly="True" />
            <asp:BoundField DataField="description" HeaderText="description" 
                SortExpression="description" ReadOnly="True" />
            <asp:TemplateField HeaderText="Charity" SortExpression="Charity">
                <EditItemTemplate>
                    <asp:DropDownList ID="ddlCharities" runat="server" 
                        DataSourceID="dsCharitiesAssignedDropDown" DataTextField="name" 
                        DataValueField="charityID">
                    </asp:DropDownList>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%# Bind("Charity") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:BoundField DataField="dateEntered" DataFormatString="{0:d}" 
                HeaderText="dateEntered" SortExpression="dateEntered" ReadOnly="True" />
            <asp:BoundField DataField="enteredBy" HeaderText="enteredBy" 
                SortExpression="enteredBy" ReadOnly="True" />
        </Columns>
    </asp:GridView>
</asp:Content>